﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UFSoft.UBF.Util.Log;

namespace UFIDA.U9C.Cust.ZL.PlugBE.Tool
{
    /*
     * HTTP接口工具类
     */
    public class HttpUitls
    {
        private static ILogger logger = LoggerManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        /*
         * get请求
         */
        public static string Get(string Url)
        {
            Log(string.Format("调用接口：{0}", Url));
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Proxy = null;
            request.KeepAlive = false;
            request.Method = "GET";
            request.ContentType = "application/json; charset=UTF-8";
            request.AutomaticDecompression = DecompressionMethods.GZip;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();

            myStreamReader.Close();
            myResponseStream.Close();

            if (response != null)
            {
                response.Close();
            }
            if (request != null)
            {
                request.Abort();
            }

            return retString;
        }

        /*
        * post请求 
        */
        public static string Post(string Url, string Data)
        {
            string retString = "";
            try
            {
                Log(string.Format("调用接口：{0}，\r\n请求值：\r\n {1}", Url, Data));

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";

                byte[] bytes = Encoding.UTF8.GetBytes(Data);
                request.ContentType = "application/json";
                request.ContentLength = bytes.Length;
                Stream myResponseStream = request.GetRequestStream();
                myResponseStream.Write(bytes, 0, bytes.Length);

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                retString = myStreamReader.ReadToEnd();

                myStreamReader.Close();
                myResponseStream.Close();

                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }

            }
            catch (Exception ex)
            {
                Log(string.Format("调用接口异常：{0}，异常信息：{1}", Url, ex.Message + ex.StackTrace));
            }
            Log(string.Format("调用接口：{0}，返回信息：\r\n{1}", Url, retString));
            return retString;
        }
        public static void Log(string msg)
        {
            logger.Error(msg);

        }


    }
}
